package enterprise.manage.service.extremetable;

import org.extremecomponents.table.context.Context;
import org.extremecomponents.table.limit.Limit;
import org.extremecomponents.table.limit.LimitFactory;
import org.extremecomponents.table.limit.TableLimit;
import org.extremecomponents.table.limit.TableLimitFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * User: libofeng
 * Date: 2007-6-29
 * Time: 10:47:07
 * Description:ExtremeTablePage
 */
public class ExtremeTablePage {
    /**
     * 该类可以继承
     */
    protected ExtremeTablePage() {
    }
    
    /**
     * 日志logback
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ExtremeTablePage.class);

    /**
     * 获得limit对象
     *
     * @param request         HttpServletRequest
     * @param defaultPageSize 默认页大小
     * @return limit对象
     */
    public static Limit getLimit(HttpServletRequest request, int defaultPageSize) {
        return ExtremeTablePage.getLimit(request, null, null, defaultPageSize);
    }

    /**
     * @param request         HttpServletRequest
     * @param parameterMap    parameterMap
     * @param tableId         tableId
     * @param defaultPageSize 默认页大小
     * @return limit对象
     */

    public static Limit getLimit(
            HttpServletRequest request
            , Map parameterMap
            , String tableId
            , int defaultPageSize) {

        Context context;
        if (parameterMap == null) {
            context = new HttpServletRequestContext(request);
        } else {
            context = new HttpServletRequestContext(request, parameterMap);
        }

        LimitFactory limitFactory;
        if (tableId == null) {
            limitFactory = new TableLimitFactory(context);
        } else {
            limitFactory = new TableLimitFactory(context, tableId);
        }

        TableLimit limit = new TableLimit(limitFactory);
        limit.setRowAttributes(Integer.MAX_VALUE, defaultPageSize);
        return limit;
    }

    /**
     * 获得limit对象，具有session及cookie记录页大小的功能
     * （需要将分页大小设置到页面<ec:parameter name="ec_crd" value="${rowsDisplayed}"/>）
     *
     * @param request         HttpServletRequest
     * @param response        HttpServletResponse
     * @param defaultPageSize 默认页大小
     * @return limit对象
     */
    public static Limit getLimit(
            HttpServletRequest request
            , HttpServletResponse response
            , int defaultPageSize) {
        return ExtremeTablePage.getLimit(request, response, null, null, defaultPageSize);
    }

    /**
     * 获得limit对象，具有session及cookie记录页大小的功能
     * （需要将分页大小设置到页面<ec:parameter name="ec_crd" value="${rowsDisplayed}"/>）
     *
     * @param request         HttpServletRequest
     * @param response        HttpServletResponse
     * @param parameterMap    parameterMap
     * @param tableId         tableId
     * @param defaultPageSize 默认页大小
     * @return limit对象
     */
    public static Limit getLimit(
            HttpServletRequest request, HttpServletResponse response
            , Map parameterMap
            , String tableId
            , int defaultPageSize) {

        Context context;
        if (parameterMap == null) {
            context = new HttpServletRequestContext(request);
        } else {
            context = new HttpServletRequestContext(request, parameterMap);
        }

        LimitFactory limitFactory;
        if (tableId == null) {
            limitFactory = new TableLimitFactory(context);
        } else {
            limitFactory = new TableLimitFactory(context, tableId);
        }

        TableLimit limit = new TableLimit(limitFactory);
        Integer currentPagesize = ExtremeRememberPagesizeUtils.getDefaultPagesize(request, defaultPageSize);
        limit.setRowAttributes(Integer.MAX_VALUE, currentPagesize);
        LOGGER.debug("当前的limit页大小是：{}", limit.getCurrentRowsDisplayed());

        //存储和传递到页面
        request.setAttribute("rowsDisplayed", limit.getCurrentRowsDisplayed());
        ExtremeRememberPagesizeUtils.saveRememberPagesize(request, response, limit.getCurrentRowsDisplayed());
        return limit;
    }
}

